package softtech.app.secretcaller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.ContentValues;

public class DatabaseUser {
	final String TAG = "DATABASE USER";
	public static final String TABLE_NAME		= "t_users";
	public static final String KEY_ID 			= "user_id";
	public static final String KEY_USERNAME 	= "username";
	public static final String KEY_PASSWORD 	= "password";
	public static final String KEY_SECRET		= "secret_key";
	public static final String KEY_PHONE		= "phone_number";
	public static final String KEY_EMAIL		= "email";
	
	private int _Id;
	private String mUsername;
	private String mPassword;
	private String mSecretKey;
	private String mPhoneNumber;
	private String mEmail;
	
	public DatabaseUser(){
		
	}
	
	public DatabaseUser(int _id , String uname , String secretKey){
		this(_id , uname , "" , secretKey , null,null);
	}
	
	public DatabaseUser(int _id , String uname, String pass, String secretKey , String phone , String email){
		this._Id = _id;
		mUsername = uname;
		mPassword = pass;
		mSecretKey = secretKey;
		mPhoneNumber = phone;
		mEmail = email;
	}

	public DatabaseUser( String uname, String pass, String secretKey , String phone , String email){
		this( -1,  uname,  pass,  secretKey ,  phone ,  email);
	}
	
	public int get_Id() {
		return _Id;
	}

	public void set_Id(int _Id) {
		this._Id = _Id;
	}

	public String getUsername() {
		return mUsername;
	}

	public void setUsername(String mUsername) {
		this.mUsername = mUsername;
	}

	public String getPassword() {
		return mPassword;
	}

	public void setPassword(String mPassword) {
		this.mPassword = mPassword;
	}

	public String getSecretKey() {
		return mSecretKey;
	}

	public void setSecretKey(String mSecretKey) {
		this.mSecretKey = mSecretKey;
	}

	public String getPhoneNumber() {
		return mPhoneNumber;
	}

	public void setPhoneNumber(String mPhoneNumber) {
		this.mPhoneNumber = mPhoneNumber;
	}

	public String getEmail() {
		return mEmail;
	}

	public void setEmail(String mEmail) {
		this.mEmail = mEmail;
	}
	
	/**
	 * parse object Database User to JSON String
	 */
	public String toJSONString(){
		JSONObject jsonObject = new JSONObject();
		try{
			jsonObject.put(KEY_ID, _Id);
			jsonObject.put(KEY_USERNAME, mUsername);
			jsonObject.put(KEY_PASSWORD, mPassword);
			if(mPhoneNumber!=null && !mPhoneNumber.equals(null) && mPhoneNumber.length() > 5){
				jsonObject.put(KEY_PHONE, mPhoneNumber);
			}
			
			if(mEmail!=null && !mEmail.equals(null) && mEmail.length() > 5){
				jsonObject.put(KEY_EMAIL, mEmail);
			}
			
			if(mSecretKey!=null && !mSecretKey.equals(null) && mSecretKey.length() == 4){
				jsonObject.put(KEY_SECRET, mSecretKey);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return jsonObject.toString();		
	}
	
	/**
	 * parse object Database User to JSON
	 */
	public JSONObject toJSONObject(){
		JSONObject jsonObject = new JSONObject();
		try{
			jsonObject.put(KEY_ID, _Id);
			jsonObject.put(KEY_USERNAME, mUsername);
			jsonObject.put(KEY_PASSWORD, mPassword);
			if(mPhoneNumber!=null && !mPhoneNumber.equals(null) && mPhoneNumber.length() > 5){
				jsonObject.put(KEY_PHONE, mPhoneNumber);
			}
			
			if(mEmail!=null && !mEmail.equals(null) && mEmail.length() > 5){
				jsonObject.put(KEY_EMAIL, mEmail);
			}
			
			if(mSecretKey!=null && !mSecretKey.equals(null) && mSecretKey.length()== 4){
				jsonObject.put(KEY_SECRET, mSecretKey);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return jsonObject;	
	}
	
	public ContentValues toContentValue(){
		ContentValues values = new ContentValues();
		values.put(KEY_ID, _Id);
		values.put(KEY_USERNAME, mUsername);
		values.put(KEY_PASSWORD, mPassword);
		
		if(mEmail!=null && !mEmail.equals(null) && mEmail.length() > 5){
			values.put(KEY_EMAIL, mEmail);
		}
		
		if(mSecretKey!=null && !mSecretKey.equals(null) && mSecretKey.length() == 4){
			values.put(KEY_SECRET, mSecretKey);
		}
		
		if(mPhoneNumber!=null && !mPhoneNumber.equals(null) && mPhoneNumber.length() > 5){
			values.put(KEY_PHONE, mPhoneNumber);
		}
		
		return values;
	}
	
	public List<NameValuePair> toListNameValuePair(){
		List<NameValuePair> listPair = new ArrayList<NameValuePair>();
		
		listPair.add(new BasicNameValuePair(KEY_ID, Integer.toString(_Id)));
		listPair.add(new BasicNameValuePair(KEY_USERNAME, mUsername));
		listPair.add(new BasicNameValuePair(KEY_PASSWORD, mPassword));
		
		
		if(mEmail!=null && !mEmail.equals(null) && mEmail.length() > 5){
			listPair.add(new BasicNameValuePair(KEY_EMAIL, mEmail));
		}
		
		if(mSecretKey!=null && !mSecretKey.equals(null) && mSecretKey.length() == 4){
			listPair.add(new BasicNameValuePair(KEY_SECRET, mSecretKey));
			
		}
		
		if(mPhoneNumber!=null && !mPhoneNumber.equals(null) && mPhoneNumber.length() > 5){
			listPair.add(new BasicNameValuePair(KEY_PHONE, mPhoneNumber));
		}
		return listPair;
	}
	
	public void setDataFromJSON(JSONObject jobject){
		try{
			_Id = jobject.getInt(KEY_ID);
			setUsername(jobject.getString(KEY_USERNAME));
			try {
				setPhoneNumber(jobject.getString(KEY_PHONE));
			} catch (Exception ex) {
				ex.printStackTrace();
			}

			try {
				setEmail(jobject.getString(KEY_EMAIL));
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			
			setSecretKey(jobject.getString(KEY_SECRET));
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
}
